package com.souku.finance.application.base;

import android.app.Application;
import android.app.Notification;
import android.os.Handler;
import android.os.Looper;

import com.android.volley.manager.RequestManager;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
import com.liulishuo.filedownloader.services.DownloadMgrInitialParams;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.net.Proxy;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by Android on 2016/12/5.
 */

public class BaseApplication extends Application {

    private static BaseApplication mContext = null;
    private static Handler mMainThreadHandler = null;
    private static Looper mMainThreadLooper = null;
    private static Thread mMainThead = null;
    private static int mMainTheadId;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        this.mMainThreadHandler = new Handler();
        this.mMainThreadLooper = getMainLooper();
        this.mMainThead = Thread.currentThread();
        this.mMainTheadId = android.os.Process.myTid();
        ShareSDK.initSDK(this);//ShareSDK 分享

        ToastUtil.init(this);
        ShareUtil.getInstance().init(this);

        //请求
        RequestManager.getInstance().init(this);

        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(this);
        ImageLoader.getInstance().init(configuration);

        //推送
        JPushInterface.setDebugMode(true);// 设置开启日志,发布时请关闭日志
        JPushInterface.init(getApplicationContext());
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL;
        JPushInterface.setPushNotificationBuilder(1, builder);

        //下载
        FileDownloadLog.NEED_LOG = false;
        FileDownloader.init(getApplicationContext(), new DownloadMgrInitialParams.InitCustomMaker()
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                        .proxy(Proxy.NO_PROXY) // set proxy
                )));

    }

    public static BaseApplication getApplication() {
        return mContext;
    }

    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    public static Looper getMainThreadLooper() {
        return mMainThreadLooper;
    }

    public static Thread getMainThread() {
        return mMainThead;
    }

    public static int getMainThreadId() {
        return mMainTheadId;
    }

}
